package commons;
/**
 * This is main class being which is called when the user clicks on log button
 */
public class CalculatorSandeep {
	 private double PRECISION = Math.pow(10, -10);
	   private double MULTIPLIER = 0.86858896;
	   
	   double x;
	   
	   double pi =  3.14159265358979323846;
	   
	   
	   public CalculatorSandeep(){}
	   
	   public CalculatorSandeep(String text) {
		   x= Double.parseDouble(text);
		
	}

	public double calculate() {
		
		if(GUI.degButton.isSelected())
           	x = x * (pi / 180);
		
		if(x<=0){
			return Double.NaN;
		}
	       int p = getP(x);
	       double result = MULTIPLIER * ln(Math.sqrt(getM(x, p))) + p;
	       return result;
	   }
	   private double ln(double x) {
	       double y = (x - 1) / (x + 1);
	       double ySqr = y * y;
	       int n = 3;
	       double result = y;
	       double num = y;
	       double term = y;
	       while (term > PRECISION) {
	           num = num * ySqr;
	           term = num / n;
	           result = result + term;
	           n += 2;
	       }
	       return result*2;
	   }
	   private double getM(double x, int p) {
	       return x / Math.pow(10, p);
	   }
	   
	   private int getP(double x) {
	       int p = 0;
	       while (x >= 10) {
	           x = x / 10;
	           p++;
	       }
	       return p;
	   }

}
